#include "lvgl.h"
#include "fr_lv_public_func.h"
#include "lv_user_sqlist.h"


#define     PAGE_MAX_LENGTH     15
#define 	CUSTOM_UI_ARRY_NUMBER   12

SqlistType_t page_list;
ElemType_t page_sqlist_space[PAGE_MAX_LENGTH]={0};

/*
page_list define:
0:ui_clock_group,
1-(PAGE_MAX_LENGTH-1):custom_ui_arry
*/
extern void fr_load_watch_face_from_ext_flash(lv_obj_t *parent,void *arg);

extern void fr_lv_86box_page1_func(lv_obj_t *parent, lv_point_t *top);
extern void fr_lv_86box_page2_func(lv_obj_t *parent, lv_point_t *top);
extern void fr_lv_86box_page3_func(lv_obj_t *parent, lv_point_t *top);
extern void fr_lv_86box_page4_func(lv_obj_t *parent, lv_point_t *top);
extern void fr_lv_86box_page5_func(lv_obj_t *parent, lv_point_t *top);
extern void fr_lv_86box_page6_func(lv_obj_t *parent, lv_point_t *top);

static const ui_func custom_ui_arry[CUSTOM_UI_ARRY_NUMBER]=
{
    fr_lv_86box_page2_func,
    fr_lv_86box_page3_func,
    fr_lv_86box_page4_func,
    fr_lv_86box_page5_func,
    fr_lv_86box_page6_func,

    // fr_lv_add_cards_page_func,
    // //---------------------------
    // fr_lv_app_exercise_func,
    // fr_lv_sleep_page_func,
    // fr_lv_weather_report_func,
    // fr_lv_pressure_measure_func,
    // fr_lv_heart_rate_list_func,
    // fr_lv_blood_oxygen_measure_func,
    // fr_lv_blood_pressure_measure_func,    
    // fr_lv_app_exercise_default_list_func,
    // //---------------------------
    // fr_lv_pressure_emotion_func,
    // fr_lv_respiratory_rate_func,
	// fr_lv_body_temperature_display_func,
    // //fr_load_watch_face_from_ext_flash,
    //---------------------------
};
static LV_ATTRIBUTE_LARGE_CONST const ui_func ui_clock_group[] =
{
    fr_lv_86box_page1_func,
	// fr_lv_main_clock2_func,
	// fr_lv_main_clock1_func,
	// fr_lv_main_clock0_func,
	// fr_lv_main_clock3_func,
	// fr_lv_main_clock4_func,
	// fr_lv_main_clock5_func,
	NULL,	//*entry_main 
};

//==============================================================
//==============================================================
//==============================================================

uint8_t fr_ui_watchface_total(void)
{
	return sizeof(ui_clock_group) / 4;
}
ui_func fr_ui_get_watchface(void)
{
	uint8_t ui_face_id = fr_system_get_watch_face();
	ui_func func = ui_clock_group[ui_face_id];
	return func;
}
void fr_lv_custom_page_set_watch_face(uint8_t watch_face_id)
{
    watch_face_id |= 0xc0;
    lv_user_list_replace(&page_list,0,watch_face_id);
}
bool fr_lv_is_ui_index_in_list(uint8_t custom_ui_arry_index)
{
    custom_ui_arry_index |= 0x80;
    uint8_t elem_id = lv_user_list_get_elem_id(&page_list,custom_ui_arry_index);
    if(elem_id != 0xff)
    {
        return 1;
    }
    else 
        return 0;
}
bool fr_lv_custom_page_add(uint8_t custom_ui_arry_index)
{
    custom_ui_arry_index |= 0x80;
    uint8_t elem_id = lv_user_list_get_elem_id(&page_list,custom_ui_arry_index);
    if( elem_id == 0xff)
    {
        printf("add elem!!\r\n");
        //列表中无该元素，则添加  
        //放在添加页面之前，添加页面永远在最后
        uint8_t len = lv_user_list_get_sqlist_length(&page_list);
        if(len == 0)
        	lv_user_list_insert(&page_list,len,custom_ui_arry_index);
        else
			lv_user_list_insert(&page_list,len-1,custom_ui_arry_index);
        
		return 1;
    }
	return 0;
}
bool fr_lv_custom_page_delete(uint8_t custom_ui_arry_index)
{
    custom_ui_arry_index |= 0x80;
    uint8_t elem_id = lv_user_list_get_elem_id(&page_list,custom_ui_arry_index);
    if(elem_id != 0xff)
    {
        lv_user_list_delete(&page_list,elem_id);
        return 1;
    }
    else 
        return 0;
}

uint8_t fr_lv_get_custom_page_length(void)
{
    return page_list.length;
}
uint8_t *fr_lv_get_custom_page_data(void)
{
    return (uint8_t *)page_list.data;
}

ui_func fr_lv_get_prev_custom_page(uint8_t index)
{
    ElemType_t prev_page = lv_user_list_get_prev_elem(&page_list,index);
    if((prev_page & 0xc0) == 0xc0)
    {
        prev_page &= 0x3f;
        return ui_clock_group[prev_page];
    }
    else
    {
        prev_page &= 0x7f;
        return custom_ui_arry[prev_page];
    }
}
ui_func fr_lv_get_next_custom_page(uint8_t index)
{
    ElemType_t next_page = lv_user_list_get_next_elem(&page_list,index);
    if((next_page & 0xc0) == 0xc0)
    {
        next_page &= 0x3f;
        return ui_clock_group[next_page];
    }
    else
    {
        next_page &= 0x7f;
        return custom_ui_arry[next_page];
    }
}
ui_func fr_lv_get_cur_custom_page(uint8_t index)
{
    ElemType_t cur_page = lv_user_list_get_cur_elem(&page_list,index);
    if((cur_page&0xc0) == 0xc0)
    {
        cur_page &= 0x3f;
        return ui_clock_group[cur_page];
    }
    else
    {
        cur_page &= 0x7f;
        return custom_ui_arry[cur_page];
    }
}
uint8_t splist_get_elem_number(void)
{
	uint8_t len = 0;
	uint8_t *custom_mode = fr_system_get_custom_page_mode();
	for (uint8_t i = 0; i < CUSTOM_ID_ARRY_NUMBER; i++)
	{
		if ((custom_mode[i] != 0) && (custom_mode[i] <= CUSTOM_ID_ARRY_NUMBER))
		{
			len++;
		}
	}
	return len;
}
//--
ui_func get_prior_element(uint8_t index)
{
    return fr_lv_get_prev_custom_page(index);
}
ui_func get_next_element(uint8_t index)
{
    return fr_lv_get_next_custom_page(index);
}
ui_func get_cur_element(uint8_t index)
{
    return fr_lv_get_cur_custom_page(index);
}
//---
void page_list_traverser(void)
{
    ListTraverse(&page_list);
}   
void fr_lv_custom_page_init(void)
{
    page_list.data = page_sqlist_space;
    page_list.length = 0;
    //----------------------------------
	uint8_t elem_len = splist_get_elem_number();
	if (elem_len == 0) // list is empty;
	{
        //clock0 and fr_lv_add_cards_page_func
        fr_lv_custom_page_add(0);
        fr_lv_custom_page_add(1);
        fr_lv_custom_page_set_watch_face(0);
        //---------------------------------
        fr_lv_custom_page_add(1);
        fr_lv_custom_page_add(2);
        fr_lv_custom_page_add(3);
        fr_lv_custom_page_add(4);
        fr_system_set_custom_page_mode();
	}
	else
	{
        uint8_t *custom_mode = fr_system_get_custom_page_mode();
		for (uint8_t i = 0; i < elem_len; i++)
		{
			fr_lv_custom_page_add(custom_mode[i]);
		}
	}
}



